package com.yegs.hospital.common;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yegs
 * @since 2020-08-20
 * @description  用于返回前端json数据的工具类
 */
public class Msg implements Serializable {

	// 状态码
	private int code;
	// 提示信息
	private String message;

	// 封装有效数据
	private HashMap data = new HashMap<String,Object>();

	private void put(String key,Object value){
		data.put(key,value);
	}

	public static Msg success() {
		Msg result = new Msg();
		result.setCode(200);
		result.setMessage("success");
		return result;
	}
	public static Msg success(Object value) {
		Msg result = new Msg();
		result.setCode(200);
		result.setMessage("success");
		result.put("data",value);
		return result;
	}

	public static Msg success(String key,Object value) {
		Msg result = new Msg();
		result.setCode(200);
		result.setMessage("success");
		result.put(key,value);
		return result;
	}
	public static Msg success(HashMap<String, Object>  map) {
		Msg result = new Msg();
		result.setCode(200);
		result.setMessage("success");
		result.setData(map);
		return result;
	}
	public static Msg success(String message) {
		Msg result = new Msg();
		result.setCode(200);
		result.setMessage(message);
		return result;
	}

	public static Msg fail() {
		Msg result = new Msg();
		result.setCode(400);
		result.setMessage("fail");
		return result;
	}

	public static Msg noPermission() {
		Msg result = new Msg();
		result.setCode(401);
		result.setMessage("no permission");
		return result;
	}

	public static Msg error() {
		Msg result = new Msg();
		result.setCode(500);
		result.setMessage("error");
		return result;
	}
	public static Msg error(String message) {
		Msg result = new Msg();
		result.setCode(500);
		result.setMessage(message);
		return result;
	}

	public static Msg unAuth(String message){
		Msg result = new Msg();
		result.setCode(403);
		result.setMessage(message);
		return result;
	}
	public static Msg unAuth(){
		Msg result = new Msg();
		result.setCode(403);
		result.setMessage("没有访问权限");
		return result;
	}


	public static Msg code(int code){
		Msg result = new Msg();
		result.setCode(code);
		result.setMessage("exception");
		return result;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public HashMap getData() {
		return data;
	}

	public void setData(HashMap data) {
		this.data = data;
	}
}
